package com.liuceng.peatio.service

import com.liuceng.BaseSpec
import com.liuceng.peatio.mapper.MemberCoefficientMapper
import com.liuceng.peatio.model.BaseDomain
import com.liuceng.peatio.model.MemberCoefficient
import spock.lang.Unroll

import java.time.LocalDate

import static spock.util.matcher.HamcrestMatchers.closeTo

/**
 * Created by liyuhang on 2016-28-4.
 */
class MemberCoefficientServiceSpec extends BaseSpec {

    def setup() {

    }

    @Unroll
    def "不限预算时可以成功获取主系数"() {
        given:
        //
        FrontCampaignService frontCampaignService = Stub()
        frontCampaignService.getDayBudget(_ as Integer, _ as Integer) >> { memberId, campaignType ->
            return null
        }
        NewRptFrontCampaignDailyService frontCampaignDailyService = Stub()
        frontCampaignDailyService.getAleadyCost(_ as Integer, _ as Integer, _ as LocalDate) >> { memberId, campaignType, rptDate ->
            if (memberId == 1 && campaignType == BaseDomain.PC && rptDate.getYear == 2016 && rptDate.getMonth == 4 && rptDate.getDay == 26) {
                return null
            } else if (memberId == 2 && campaignType == BaseDomain.PC && rptDate.getYear == 2016 && rptDate.getMonth == 4 && rptDate.getDay == 26) {
                return new BigDecimal(10)
            } else if (memberId == 3 && campaignType == BaseDomain.PC && rptDate.getYear == 2016 && rptDate.getMonth == 4 && rptDate.getDay == 26) {
                return new BigDecimal(20)
            } else if (memberId == 4 && campaignType == BaseDomain.PC && rptDate.getYear == 2016 && rptDate.getMonth == 4 && rptDate.getDay == 26) {
                return null
            } else if (memberId == 5 && campaignType == BaseDomain.PC && rptDate.getYear == 2016 && rptDate.getMonth == 4 && rptDate.getDay == 26) {
                return null
            }
            return null
        }

        and:
        MemberCoefficientService service =
                new MemberCoefficientService(
                        frontCampaignService: frontCampaignService,
                        frontCampaignDailyService: frontCampaignDailyService)
        Map<Integer, MemberCoefficient> coeMap = [
                1: new MemberCoefficient(memberId: 1, platform: BaseDomain.PC, rangeMark: "20", values: "1,4,3.5,3"),
                2: new MemberCoefficient(memberId: 2, platform: BaseDomain.PC, rangeMark: "20,30", values: "1,5,4.5,4,3.5,2.7"),
                3: new MemberCoefficient(memberId: 3, platform: BaseDomain.PC, rangeMark: "", values: "1,5"),
                4: new MemberCoefficient(memberId: 4, platform: BaseDomain.PC, rangeMark: "20,30", values: "1,5,4.5,4,3.5,2.7"),
                5: new MemberCoefficient(memberId: 5, platform: BaseDomain.PC, rangeMark: "", values: "1,5")
        ]

        expect:
        def c = service.getMajorCoefficient(mId, needCalculateOriginCost, customerNum, coeMap, BaseDomain.PC, LocalDate.of(2016, 4, 26))
        c closeTo(majorCoe, 0.01)


        where:
        mId | needCalculateOriginCost | customerNum || majorCoe
        1   | new BigDecimal(5000)    | 10          || 3
        2   | new BigDecimal(10)      | 0           || 1
        3   | new BigDecimal(25)      | 1           || 5
        4   | new BigDecimal(60)      | 2           || 3.5
        5   | new BigDecimal(0)       | 1           || 1
    }

    def "获取所有客户下各个渠道系数的一个大表映射pc和mobile" () {
        given:
        MemberCoefficientMapper memberCoefficientMapper = Stub();
        memberCoefficientMapper.findMemberCoefficient(_ as Map<String, Integer>) >> { params ->
            for (Map<String, Integer> m : params) {
                if (m.get("memberId") == 1) {
                    List<MemberCoefficient> memberCoefficientPc = new ArrayList<>();
                    MemberCoefficient bean = new MemberCoefficient();
                    bean.setMediumId(0);
                    MemberCoefficient bean1 = new MemberCoefficient();
                    bean.setMediumId(1);
                    memberCoefficientPc.add(bean);
                    memberCoefficientPc.add(bean1);
                    return memberCoefficientPc;
                }
                if (m.get("memberId") == 2) {
                    List<MemberCoefficient> memberCoefficientPc = new ArrayList<>();
                    MemberCoefficient bean = new MemberCoefficient();
                    bean.setMediumId(0);
                    MemberCoefficient bean1 = new MemberCoefficient();
                    bean.setMediumId(1);
                    memberCoefficientPc.add(bean);
                    memberCoefficientPc.add(bean1);
                    return memberCoefficientPc;
                }
                if (m.get("memberId") == 3) {
                    List<MemberCoefficient> memberCoefficientPc = new ArrayList<>();
                    return memberCoefficientPc;
                }
            }

        }

        List<Map<String, Integer>> memberId1 = new ArrayList<>();
        Map<String, Integer> map1 = ["member_id":1]
        Map<String, Integer> map2 = ["member_id":2]
        Map<String, Integer> map3 = ["member_id":3]
        memberId1.add(map1);
        memberId1.add(map2);
        memberId1.add(map3);

        List<Map<String, Integer>> memberId2 = new ArrayList<>();
        Map<String, Integer> map4 = ["member_id":1]
        Map<String, Integer> map5 = ["member_id":2]
        memberId2.add(map4);
        memberId2.add(map5);

        List<Map<String, Integer>> memberId3 = new ArrayList<>();
        Map<String, Integer> map6 = ["member_id":3]
        memberId3.add(map6);

        MemberCoefficientService service = new MemberCoefficientService(memberCoefficientMapper:memberCoefficientMapper);

        when:
        Map<Integer, Map<Integer, MemberCoefficient>> maps1 = service.findMemberCoefficientPcMap(memberId1);
        then:
        maps1.size() == 2

        when:
        Map<Integer, Map<Integer, MemberCoefficient>> maps2 = service.findMemberCoefficientPcMap(memberId2);
        then:
        maps2.size() ==2

        when:
        Map<Integer, Map<Integer, MemberCoefficient>> maps3 = service.findMemberCoefficientPcMap(memberId3);
        then:
        maps3.size() ==0

    }
}